package titt.service.proxy;

import titt.io.IOFactory;
import titt.io.socket.TITTSocket;
import titt.service.invoke.AdvancedMethodInvoker;
import titt.type.Type;
import titt.type.TypedObject;

public class TITTInvoker implements AdvancedMethodInvoker {

	private final TITTRequester tr;
	

	public TITTInvoker(String... tokens) {
		this.tr = new TITTRequester(tokens);
	}

	public TypedObject call(Type ret, String name, TypedObject... args) throws Exception {

		TITTSocket ts = IOFactory.createTITTSocket();
		try {

			tr.sendRequest(ts, name, args);

			TypedObject t = ret == null? tr.readResponse(ts) : tr.readResponse(ts, ret);

			ts.close();

			return t;

		}
		catch(Exception e) {

			ts.close();
			throw e;

		}
	}

	public TypedObject call(String name, TypedObject... args) throws Exception {
		return call(null, name, args);
	}


}
